// pages/home/me.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderMenu: [{
      id: '0',
      title: '全部订单',
      icon: '/images/icon_not_pay.png',
      num: 0,
      targetUrl: '/pages/order/list?index=0',
    },
    {
      id: '1',
      title: '未派单',
      icon: '/images/icon_not_deliver.png',
      num: 0,
      targetUrl: '/pages/order/list?index=1'
    },
    {
      id: '2',
      title: '已派单',
      icon: '/images/icon_not_receive.png',
      num: 0,
      targetUrl: '/pages/order/list?index=2'
    },
    {
      id: '3',
      title: '已完成',
      icon: '/images/icon_not_receive.png',
      num: 0,
      targetUrl: '/pages/order/list?index=3'
    },

    ],

    myinfo: {},
    uid: 0,
    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    nickName: '点击显示微信头像',
    if_register: false,
    openid: '',
    session_key: '',
    service_list: [
      // {
      //   title: '会员中心',
      //   icon: '/images/个人中心图片/付费会员.png',
      //   num: 0,
      //   page_url: '/pages/vip/vip',
      // },
      {
        title: '商家中心',
        icon: '/images/个人中心图片/积分中心.png',
        num: 0,
        page_url: '/pages/shop/shop_center',
      },
      {
        title: '分销中心',
        icon: '/images/个人中心图片/联系客服.png',
        num: 0,
        page_url: '/pages/distribution/distribution_center',
      },
      {
        title: '优惠券列表',
        icon: '/images/个人中心图片/优惠券.png',
        num: 0,
        page_url: '/pages/ticket/myticket_list',
      },
      {
        title: '我的收藏',
        icon: '/images/个人中心图片/我的收藏.png',
        num: 0,
        page_url: '/pages/collection/collection',
      },
      {
        title: '地址信息',
        icon: '/images/个人中心图片/地址信息.png',
        num: 0,
        page_url: '/pages/address/address',
      },
      // {
      //   title: '我的余额',
      //   icon: '/images/个人中心图片/我的余额.png',
      //   num: 0,
      //   page_url: '/pages/blank/blank',
      // },
      // {
      //   title: '砍价记录',
      //   icon: '/images/个人中心图片/砍价记录.png',
      //   num: 0,
      //   page_url: '/pages/blank/blank',
      // }
    ],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init_data(options)
    this.get_myinfo()
  },

  getPhoneNumber(e) {
    let that = this
    let post_data = {
      uid: this.data.uid,
      code: e.detail.code
    }
    util.showToast("正在注册")
    request.post(api.setphone, post_data).then(res => {
      this.check_register(res.etype)
    }).catch(e => {
      util.showToast('获取手机失败')
    })
  },

  check_register(etype) {
    if (etype != 0) {
      util.showToast('认证成功')
      setTimeout(() => {
        this.go_shop_center()
      }, 1500);
    }
    else {
      util.showToast("认证失败")
      setTimeout(() => {
        util.goBack()
      }, 1500);
    }
  },

  go_shop_center() {
    wx.navigateTo({
      url: '/pages/shop/shop_center',
    })
  },

  go_expert() {
    wx.navigateTo({
      url: '/pages/expert/expert_center',
    })
  },

  get_order_list_number(length) {
    let orderMenu = this.data.orderMenu
    orderMenu[0].num = length.all_count
    orderMenu[1].num = length.order_status1
    orderMenu[2].num = length.order_status2
    orderMenu[3].num = length.order_status5
    this.setData({
      orderMenu
    })
  },

  init_data(options) {
    let uid = util.getAppValue(App.globalData.uid)
    if (!uid) {
      uid = 1
    }
    this.setData({
      if_register: App.globalData.if_register,
      uid,
      options,
    })
    let avatarUrl = App.globalData.userInfo.avatarUrl
    let nickName = App.globalData.userInfo.nickName
    if (avatarUrl) {
      this.setData({
        avatarUrl,
        nickName
      })
    }
  },

  get_myinfo() {
    let params = {
      uid: this.data.uid
    }
    request.post(api.myinfo, params).then(res => {
      this.setData({
        myinfo: res
      })
    })
  },
  go_order_another(e) {
    let url = e.currentTarget.dataset.url + '&from=manager'
    wx.navigateTo({
      url,
    })
  },

  go_order() {
    let url = '/pages/order/list' + '?from=manager'
    wx.navigateTo({
      url
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})